package com.anis.project.medcare.editors;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import javax.swing.JRootPane;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.internal.theme.RangeDrawData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.osgi.framework.Bundle;

import com.anis.jpa.medical.model.Common;
import com.anis.jpa.medical.model.MedicalReport;
import com.anis.jpa.medical.model.Patient;
import com.anis.jpa.medical.model.Templates;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.editors.input.ReportEditorInput;
import com.anis.project.medcare.ooswriter.OOoSwtViewer;
import com.anis.project.medcare.save.report.SaveReportDialog;
import com.sun.star.comp.beans.NoConnectionException;
import com.sun.star.comp.beans.SystemWindowException;
import com.sun.star.lang.EventObject;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.util.CloseVetoException;
import com.sun.star.util.XModifyListener;

public class ReportEditor extends EditorPart {

	private static final String NEW_WRITTER_DOCUMENT = "private:factory/swriter";

	private OOoSwtViewer viewer;

	public static String ID = "com.anis.project.medcare.report.editor";

	private ReportEditorInput medicalReportInput;

	private boolean isModified;

	private URL fileURL;

	public ReportEditor() {

	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		Patient patient = medicalReportInput.getPatient();
		MedicalReport radioReport = medicalReportInput.getRadioReport();
		byte[] serializedDocument = viewer.storeDocumentToByteArray();
		radioReport.setReportcontent(serializedDocument);
		Date date = new Date();
		radioReport.setReportdate(date);
		radioReport.setReportType(Common.MEDREPTYPE);
		if (patient == null) {
			Shell parentShell = Activator.getDefault().getWorkbench()
					.getActiveWorkbenchWindow().getShell();
			SaveReportDialog searchDialog = new SaveReportDialog(radioReport,null,
					parentShell);
			searchDialog.open();
		} else {
			radioReport.setReportcontent(serializedDocument);
			Activator.getDefault().databaseBinder()
					.savePatientReport(patient, radioReport);
		}
		isModified = false;
		firePropertyChange(PROP_DIRTY);
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		if (input instanceof ReportEditorInput) {
			this.medicalReportInput = (ReportEditorInput) input;
		} else
			throw new RuntimeException("Wrong Input");
		setSite(site);
		setInput(medicalReportInput);

	}

	@Override
	public boolean isDirty() {
		return isModified;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {

		Composite reportingComposite = new Composite(parent, SWT.NO_BACKGROUND
				| SWT.EMBEDDED);
		reportingComposite.setLayout(new GridLayout(1, false));

		try {
			System.setProperty("sun.awt.noerasebackground", "true");
		} catch (NoSuchMethodError error) {
		}

		/* Create and setting up frame */
		Frame frame = SWT_AWT.new_Frame(reportingComposite);
		Panel panel = new Panel(new BorderLayout()) {
			public void update(java.awt.Graphics g) {
				paint(g);
			}
		};
		frame.add(panel);
		JRootPane root = new JRootPane();
		panel.add(root);
		java.awt.Container contentPane = root.getContentPane();

		viewer = new OOoSwtViewer();
		contentPane.add(viewer);
		try {
			byte[] radioReportContent = medicalReportInput.getReportContent();
			if (null != radioReportContent)
				viewer.setDocumentBinary(radioReportContent);
			else {
				List<Templates> templates = Activator.getDefault()
						.databaseBinder().retrieveTemplate(Common.MEDREPTEMP);
				if (!templates.isEmpty()) {
					Templates temp = templates.get(0);
					radioReportContent = temp.getMedicalReportTemplate();
				} else {
					MessageDialog
							.openError(parent.getShell(), "No template Found",
									"Please specify a template report in the preference !");
				}
			}
			if (radioReportContent != null)
				viewer.setDocumentBinary(radioReportContent);

			viewer.getDocument().addModifyListener(new XModifyListener() {

				@Override
				public void disposing(EventObject arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void modified(EventObject arg0) {
					isModified = true;
					firePropertyChange(PROP_DIRTY);
				}
			});

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public void printDocument() {
		viewer.printDocument();
	}
}
